﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows;
using AccountManagement.BussinessObjects;
using AccountManagement.DataAccess;

namespace AccountManagement
{
    /// <summary>
    /// Interaction logic for FrmAccountBalance.xaml
    /// </summary>
    public partial class FrmAccountBalance : Window
    {
        private List<tblAccount> _lstAccounts;
        private decimal _oldValue;

        public FrmAccountBalance()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                var objIncomingBO = new AccountsBO();
                _lstAccounts = objIncomingBO.GetAll();

                dgAccounts.ItemsSource = _lstAccounts;
            }
            catch (Exception)
            {
                
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void dgAccounts_RowEditEnding(object sender, Microsoft.Windows.Controls.DataGridRowEditEndingEventArgs e)
        {
            try
            {
                var editingRow = (tblAccount)e.Row.Item;

                //var regEx = new Regex(@"^(((\d{1,3})(,\d{3})*)|(\d+))(.\d+)?$"); // validates numbers, with or without decimal places, and comma 1000 separators.

                //if (!regEx.IsMatch(editingRow.SoDu.ToString()))
                //{
                //    MessageBox.Show("Chỉ được phép nhập số.");
                //    ((tblAccount)e.Row.Item).SoDu = _oldValue;
                //}

                var accountsBO = new AccountsBO();
                int ret = accountsBO.Update(editingRow, editingRow.SoDu - _oldValue);

                if (ret == Constants.Errors.UNKNOW)
                {
                    MessageBox.Show("Có lỗi xảy ra khi cập nhật số dư tài khoản.");
                    ((tblAccount) e.Row.Item).SoDu = _oldValue;
                }

                //var objIncomingBO = new AccountsBO();
                //_lstAccounts = objIncomingBO.GetAll();

                //dgAccounts.ItemsSource = _lstAccounts;
            }
            catch (Exception)
            {
                
            }
        }

        private void dgAccounts_BeginningEdit(object sender, Microsoft.Windows.Controls.DataGridBeginningEditEventArgs e)
        {
            try
            {
                _oldValue = ((tblAccount) e.Row.Item).SoDu;
            }
            catch (Exception)
            {
                
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var objIncomingBO = new AccountsBO();
                _lstAccounts = objIncomingBO.GetAll();

                dgAccounts.ItemsSource = _lstAccounts;
            }
            catch (Exception)
            {

            }
        }
    }
}
